package com.ptt.sqm.activity;

import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ptt.sqm.component.MenuList;
import com.ptt.sqm.model.AwardInfoList;
import com.ptt.sqm.model.CustomerInfo;
import com.ptt.sqm.model.Login;
import com.ptt.sqm.model.RoleMenu;
import com.ptt.sqm.session.SessionManager;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqm.util.DateDeserializer;
import com.ptt.sqm.util.ListDeserializer;
import com.ptt.sqm.util.WidgetUtil;
import com.ptt.sqm.ws.WebserviceCall;
import com.ptt.sqmonmobile.R;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DefaultFragment extends Fragment {

	// Session Manager
	SessionManager session;

	// Create Menu List
	MenuList menuList;

	// Create Webservice class object
	WebserviceCall com;
	Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateDeserializer()).registerTypeAdapter(List.class, new ListDeserializer()).create();

	private CharSequence mTitle;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		session = new SessionManager(getActivity().getApplicationContext(), getActivity());

		WebserviceCall.reset();
		com = WebserviceCall.getInstance(getString(R.string.ws_url), getString(R.string.ws_namespace), session, getActivity());

		// hide keyboard
		WidgetUtil.setupSoftKeyboardClosed(getActivity(), view);
		
		
		// set Title
		mTitle = getActivity().getTitle();

		menuList = MenuList.getInstance();

		if (getArguments() != null) {
			onSectionAttached(getArguments().getInt(AppUtil.ARG_SECTION_NUMBER));
			restoreActionBar();
		}

	}

	public void onSectionAttached(int number) {
		if (number == 0)
			mTitle = getString(R.string.title_section0);

		else if (number == menuList.getPosFrm_ShowAssessData())
			mTitle = getString(R.string.title_section1);

		else if (number == menuList.getPosFrm_AssessData())
			mTitle = getString(R.string.title_section2);

		else if (number == menuList.getPosFrm_StationInfo())
			mTitle = getString(R.string.title_section3);

		else if (number == menuList.getPosRpt_AssessComment())
			mTitle = getString(R.string.title_section4);

		else if (number == menuList.getPosRpt_AssessPoint())
			mTitle = getString(R.string.title_section5);

		else if (number == menuList.getPosLogout())
			mTitle = getString(R.string.title_section6);
	}

	public void restoreActionBar() {
		ActionBar actionBar = getActivity().getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}
	
	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		Log.i("Session" + this, "on Detach");
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	
}
